<?php
/**
 * Created by PhpStorm.
 * User: cz
 * Date: 2017/4/11
 * Time: 22:05
 */
namespace Common\Model;
use Think\Exception;
use Think\Model;
use Com\JSSDK;
class UserInfoModel extends Model{
    private  $appid="wx7a40b9a086f0bd81";
    private  $appSecret="d29d30a9709956940dd1c8146407ffaa";
    private  $WechatAuth="";//初始化WechatAuth类
    private  $access_token="";//缓存token
    //获取问题内容
    public function getQuestion(){
        $questionContent = M("userContent");
        //status = 1的问题就下线
        $questionArr = $questionContent->where("status = 0")->field("id,active_parent_id,active_name,type,radio_name")->select();
        //return $questionArr;
        $arr1 = array();
        $arr2 = array();
        $arr3 = array();
        for($i = 0; $i < count($questionArr);$i++){
            switch($questionArr[$i]['type']){
                case "title":
                    $arr1[] = $questionArr[$i];
                    break;
                case "subtitle":
                    $arr2[] = $questionArr[$i];
                    break;
                case "radio":
                    $arr3[] = $questionArr[$i];
                    break;
                default:
                    break;
            }
        }
        for($i = 0; $i < count($arr1); $i++){
            $list = array();
            for($j = 0; $j < count($arr2); $j++){
                if($arr2[$j]["active_parent_id"] == $arr1[$i]["id"]){
                    $list3 = array();
                    for($k = 0; $k < count($arr3); $k++){
                        if($arr3[$k]["active_parent_id"] == $arr2[$j]["id"]){
                            $list3[]=$arr3[$k];
                            //return $list3;
                        }
                    }
                    $arr2[$j]["lists"]=$list3;
                    $list[]=$arr2[$j];
                }
            }
            $arr1[$i]["lists"]=$list;
        }
        return $arr1;
/*
        //性能差的写法
        $questionArr = $questionContent->where("active_parent_id = 0 AND status = 0")->field("id,active_parent_id,active_name,type,radio_name")->select();
        $webToArr = array();
        if(is_array($questionArr) && !empty($questionArr)){
            for($i = 0;$i < count($questionArr); $i++){
                $sub = array();
                $webArr = array();
                $webArr = $questionArr[$i];
//                $webArr["id"] = $questionArr[$i]["id"];
//                $webArr["type"] = $questionArr[$i]["type"];
//                $webArr["radio_name"] = $questionArr[$i]["radio_name"];
//                $webArr["active_parent_id"] = $questionArr[$i]["active_parent_id"];
//                $webArr["title"] = $questionArr[$i]["active_name"];
                $webArr["sub"] = array();
                $subArr = $questionContent->where("active_parent_id = ".$questionArr[$i]["id"]." AND status = 0")->field("id,active_name,radio_name,type,active_parent_id")->select();
                for($j = 0;$j < count($subArr); $j++) {
                    $arr = array();
                    $arr["id"] = $subArr[$j]["id"];
                    $arr["type"] = $subArr[$j]["type"];
                    $arr["title"] = $subArr[$j]["active_name"];
                    $arr["radio_name"] = $subArr[$j]["radio_name"];
                    $arr["active_parent_id"] = $subArr[$j]["active_parent_id"];
                    $radioArr = $questionContent->where("active_parent_id = " . $subArr[$j]["id"] . " AND status = 0")->field("id,active_name,type,radio_name,active_parent_id")->select();
                    $arr["radio"] = $radioArr;
                    //array_push($sub["radioArr"],$arr);
                    $sub[$j] = $arr;
                }
                //array_push($webArr["subtitle"],$sub);
                $webArr["sub"][] = $sub;
                $webToArr[] = $webArr;
            }
            return $webToArr;
        }*/

    }
    //插入数据库信息
    public function customerData( $userData = array() ){
        $customerInfo = M("userInfo");
        if (!$customerInfo->autoCheckToken($userData)){
            return show(1,'不能重复提交',"");
        }
        $mediaId = $userData['serverId'];
        foreach($userData as $k=>$value){
            $data[$k] = $value;
        }
        $myUri = (empty($_SERVER['HTTPS']))?'http://':'https://';
        if($mediaId){
            $this->WechatAuth = new JSSDK($this->appid,$this->appSecret);
            $JSSDK = $this->WechatAuth;
            $this->access_token = $JSSDK->getAccessToken();
            $imageAddr = $this->getmedia($this->access_token,$mediaId,"cz");
            $res = getimagesize($imageAddr);
            if(!$res){
                $SqlAdd = "/shengbei/shengbei_active/Public/images/shengbei.jpg";
            }else{
                $SqlAdd = explode($_SERVER['DOCUMENT_ROOT'],$imageAddr)[1];
            }
        }else{
            $SqlAdd = "/shengbei/shengbei_active/Public/images/shengbei.jpg";
        }
        $data['shopId']         = session("shopId");
        $data['media_id']       = $mediaId;
        $data['access_token']   = $this->access_token;
        $data['customer_image'] = $myUri.$_SERVER['SERVER_NAME'].$SqlAdd;
        $resultId = $customerInfo->add($data);
        if($resultId){
            return show(1,'提交成功了哦',"");
        }else{
            return show(0,'提交失败',"");
        }
    }
    //判断是否审核通过
    public function checkUserPass($infoId = "",$checkId=""){
        if(empty($infoId) || empty($checkId) || (!is_numeric($checkId))){
            return show(0,'审核失败',"");
        }
        //$checkId = 2代表审核通过
        //$checkId = 1代表审核未通过
        if($checkId == 2){
            $status = 2;
        }else if($checkId == 1){
            $status = 1;
        }else{
            $status = 0;
        }
        $checkInfo = M("userInfo");
        $data['status'] = $status;
        $res = $checkInfo->where("id = ".$infoId)->save($data);
        if($res){
            return show(1,'操作成功',"");
        }else{
            return show(0,'操作失败',"");
        }
    }
    //后台需要的列表内容
    public function getAdminInfo(){
        $adminInfo = M("userInfo");
        $result = $adminInfo->field("id,user_name,user_age,user_mobile,user_sex,customer_image,customer_addr,status")->select();
        return $result;
    }
    //前台后台获取个人单条信息
    public function getIdForUserInfo($infoId = 1){
        $getInfo = M("userInfo");
        $res = $getInfo->where("id = ".$infoId)->select();
        return $res;
    }
    //审核中方法
    public function getUserInfoDetail(){
        //status状态 0是审核中   1是未通过    2是审核通过
        $customerInfo = M("userInfo");
        $result = $customerInfo->where('shopId= '.session("shopId").' AND status=0')->getField("id,user_name,user_age,user_mobile");
        return $result;
    }
    //未通过方法
    public function getUserInfoFalse(){
        //status状态 0是审核中   1是未通过    2是审核通过
        $customerInfo = M("userInfo");
        $result = $customerInfo->where('shopId= '.session("shopId").' AND status=1')->getField("id,user_name,user_age,user_mobile");
        return $result;
    }
    //审核通过方法 status =2
    public function getUserInfoPass(){
        //status状态 0是审核中   1是未通过    2是审核通过
        $customerInfo = M("userInfo");
        $result = $customerInfo->where('shopId= '.session("shopId").' AND status=2')->getField("id,user_name,user_age,user_mobile");
        return $result;
    }
    private function getmedia($access_token,$media_id,$foldername){
        $pro = explode(__ROOT__,dirname(__file__))[0];
        //print_r(is_writable ( $pro.__ROOT__."/Uploads/User_cert/".$foldername ));die();
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;
        //print_r(is_writable ( __ROOT__."/Uploads/User_cert/".$foldername ));
        if (!file_exists($pro.__ROOT__."/Uploads/User_cert/".$foldername)) {
            mkdir($pro.__ROOT__."/Uploads/User_cert/".$foldername, 0777, true);
        }
        $targetName = $pro.__ROOT__.'/Uploads/User_cert/'.$foldername.'/'.date('YmdHis').rand(1000,9999).'.jpg';
        $ch = curl_init($url); // 初始化
        $fp = fopen($targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置，值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return $targetName;
    }
}